/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * Manages state invariant.
 *
 * @author David Servat
 * @since 10 apr. 07
 */
public class StateInvariantSection extends PropertyViewSection{

	/**
	 * The Class DirectEditFocusListener.
	 */
	protected class DirectEditFocusListener implements org.eclipse.swt.events.FocusListener{

		/**
		 * Focus gained.
		 * 
		 * @param e the e
		 */
		public void focusGained(FocusEvent e){}

		/**
		 * Focus lost.
		 * 
		 * @param e the e
		 */
		public void focusLost(FocusEvent e){
			if(!stateInvariantText.isDisposed()){
				if((umlElement != null) && (umlElement instanceof State)){			
					if(!stateInvariantText.getText().trim().equals("")){
						String stateInvariant = "[ " + stateInvariantText.getText().trim() + " ]";
						LiteralString ls = UMLFactory.eINSTANCE.createLiteralString();
						ls.setValue(stateInvariant);

						//test the existence of a state invariant
						State state = (State)umlElement;
						Constraint stInv = state.getStateInvariant();
						if(stInv == null){
							stInv = UMLFactory.eINSTANCE.createConstraint();
							stInv.setName("StateInvariant");
						}
						stInv.setSpecification(ls);
						state.setStateInvariant(stInv);
					}
					else{
						//test the existence of a state invariant
						State state = (State)umlElement;
						Constraint stInv = state.getStateInvariant();
						if(stInv != null){
							state.setStateInvariant(null);
							state.getOwnedRules().remove(stInv);
						}
					}
				}
			}
		}
	}

	/**
	 * The state invariant text.
	 */
	private Text stateInvariantText;

	/**
	 * The direct edit focus listener.
	 */
	private DirectEditFocusListener directEditFocusListener = new DirectEditFocusListener();

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);

		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		stateInvariantText = this.getWidgetFactory().createText(composite, "");
		stateInvariantText.setEditable(true);
		// nameText.addFocusListener(directEditFocusListener);
		data = new FormData();
		data.width = 150;
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		stateInvariantText.setLayoutData(data);

		stateInvariantText.addFocusListener(directEditFocusListener);

		CLabel nameLabel = this.getWidgetFactory().createCLabel(composite, "Invar.:");
		nameLabel.setToolTipText("StateInvariant");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(stateInvariantText, -ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(stateInvariantText, 0, SWT.CENTER);
		nameLabel.setLayoutData(data);
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	@Override
	public void refresh() {
		if (!stateInvariantText.isDisposed()){
			if(umlElement instanceof State){
				stateInvariantText.removeFocusListener(directEditFocusListener);
				if(((State)umlElement).getStateInvariant() != null){
					Constraint stInv = ((State)umlElement).getStateInvariant();
					if(stInv.getSpecification() instanceof LiteralString){
						LiteralString ls = (LiteralString)stInv.getSpecification();
						String stInvSring = ls.stringValue();
						stateInvariantText.setText(stInvSring.substring(stInvSring.indexOf("[") + 1, stInvSring.lastIndexOf("]")).trim());
//						if(graphicalElement != null){
//							if(graphicalElement instanceof StateEditPart){
//								TransitionGuardAndAllDisplayEditPart guardAndAll = ((TransitionEditPart)graphicalElement).getGuardAndAllDisplayEditPart();
//								GraphNode gN = guardAndAll.getGraphNode();
//								if((gN != null) && (gN.getProperty(EdgeTransitionHelper.TRANSITION_DISPLAY_GUARD) != null))
//									gN.getProperty(EdgeTransitionHelper.TRANSITION_DISPLAY_GUARD).setValue(EdgeTransitionHelper.TRANSITION_DISPLAY);
//							}
//						}
					}
					else{
						stateInvariantText.setText("");
					}
				}
				else{
					stateInvariantText.setText("");
				}
				stateInvariantText.addFocusListener(directEditFocusListener);
			}				
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if(stateInvariantText!=null && !stateInvariantText.isDisposed())
			stateInvariantText.removeFocusListener(directEditFocusListener);
	}
}